/**
 * JungoMobileWeb
 * ApiController.java
 * 
 * Copyright 2013 Teachizen
 * Written by cloudstick
 *	2013. 8. 8. first written.
 *
 * note:
 */
package com.jungo.web.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jungo.web.bean.ApiResult;
import com.jungo.web.bean.User;
import com.jungo.web.dao.LoginDao;
import com.jungo.web.util.JsonUtil;

/**
 * @author cloudstick
 *
 */
@Controller
@Slf4j
public class ApiController {
	
	@Autowired
	private LoginDao loginDao;
	
	@RequestMapping("/api/updateRegistration.do")
	public void isUser(@RequestParam("deviceId") String deviceId,
						@RequestParam("RegistrationId") String RegistrationId,
						HttpServletResponse response) {
		
		User user = new User();
		user.setDevice_id(deviceId);
		user.setRid(RegistrationId);
		log.info("Update registration id for user[{}]", user);
		
		ApiResult apiResult = new ApiResult();
		apiResult.setResult("SUCCESS");
		apiResult.setMessage("SUCCESS");
		
		try {
			loginDao.updateRegistrationId(user);
		} catch(Exception e) {
			log.error("Exception occured whiling update registrationId to user[{}]", user, e);
			apiResult.setResult("FAIL");
			apiResult.setMessage(e.getMessage());
		}
		
		String jsonMessage = JsonUtil.getJsonString(apiResult);
		
		try {
			response.getWriter().write(jsonMessage);
		} catch (IOException e) {
			log.error("Exception occured whiling response result[{}]", apiResult);
		}
		
	}
}
